﻿$(function () {

    procuredFan()//采购详情
});
var purchaseNumbers = ""//localStorage["purchaseNumber"]
//采购详情
function procuredFan() {
    var procuredId = localStorage["procuredparticularsID"]
    $.ajax({
        url: "/RawMaterial/ProcuredMondel",
        type: "get",
        data: {
            Id: procuredId,
        },
        success: function (data) {
            data = JSON.parse(data);
            console.log(data.data);
            // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
            $(data.data).each(function () {
                $("#procuredId").text(this.procuredId)
                $("#ProcuredNumber").text(this.procuredNumber)
                $("#FeedstockNumber").text(this.feedstockNumber)
                $("#FeedstockName").text(this.feedstockName)
                $("#FeedstockNum").text(this.feedstockNum)
                $("#Feedstocktype").text(this.feedstocktype)
                $("#FeedstockSize").text(this.feedstockSize)
                var now = new Date(this.addTime);
                var year = now.getFullYear()
                var month = ("0" + now.getMonth() + 2).slice(-2);
                var day = ("0" + now.getDate()).slice(-2);
                $("#AddTime").text(year + "-" + month + "-" + day);
                $("#ProcuredBudget").text(this.procuredBudget)
                $("#PayMethods").text(this.payMethods)
                $("#LotNumber").text(this.lotNumber)
                $("#ProcessPrice").text(this.processPrice)
                $("#processActual").text(this.processActual)
                $("#procuredState").val(this.procuredState)
                if (this.procuredState == 0) {
                    $("#procuredState").text("待采购")
                } else {
                    $("#procuredState").text("已采购")
                }
                purchaseNumbers = this.procuredNumber
                localStorage["storageprocuredId"] = procuredId

            })
            //采购供应商
            warehouseFan(data.data.supplierID)

        }
    });

}
//采购供应商
function warehouseFan(ID) {
    $.ajax({
        url: "/Supplier/LedgerInfoSupplierFan",
        type: "get",
        data: {
            supplierID: ID,
        },
        success: function (data) {
            data = JSON.parse(data);
            console.log(data.data);
            // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
            $(data.data).each(function () {
                $("#supplierNumber").text(this.supplierNumber)
                $("#supplierName").text(this.supplierName)
                $("#supplierPrincipal").text(this.supplierPrincipal)
                $("#supplierPhone").text(this.supplierPhone)
            })
        }
    });
    //物流信息
    LogisticsFan()
}
//物流信息
function LogisticsFan() {
    layui.use('layer', function () {
        var form = layui.form;
        // 创建表格实例
        $.ajax({
            url: "/Supplier/LedgerInfoLogisticsFan",
            type: "get",
            data: {
                purchaseNumber: purchaseNumbers,
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data);
                console.log(data.data.logisticsType);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                if (data.data.logisticsType == 1) {
                    $("#Sendout1").css("color", "blue");
                    $("#Sendout2").css("color", "blue");
                    $("#Sendout3").css("border-color", "blue");
                    $("#middling1").css("color", "aqua");
                    $("#middling2").css("color", "dimgray");
                    $("#middling3").css("border-color", "aqua");
                    $("#Goods1").css("color", "aqua");
                    $("#Goods2").css("color", "dimgray");
                    $("#orderNumber1").text(data.data.orderNumber);

                    var creation = formatTime(data.data.creationTime)
                    $("#creationTime1").text(creation);

                    var payment = formatTime(data.data.paymentTime)
                    $("#paymentTime1").text(payment);

                    $("#chet1").empty();
                    $("#chet1").append(
                        '<button type = "button" class= "layui-btn layui-btn-normal layui-btn-radius" onclick = "toGoods(' + data.data.orderID + ')" >紧急发货</button >'
                    );
                }
                if (data.data.logisticsType == 2) {
                    $("#Sendout1").css("color", "blue");
                    $("#Sendout2").css("color", "blue");
                    $("#Sendout3").css("border-color", "blue");
                    $("#middling1").css("color", "blue");
                    $("#middling2").css("color", "blue");
                    $("#middling3").css("border-color", "blue");
                    $("#Goods1").css("color", "aqua");
                    $("#Goods2").css("color", "dimgray");

                    $("#orderNumber1").text(data.data.orderNumber);

                    var etal = formatdate(data.data.eTA)
                    $("#eTA1").text(etal);

                    var creation = formatTime(data.data.creationTime)
                    $("#creationTime1").text(creation);

                    var payment = formatTime(data.data.paymentTime)
                    $("#paymentTime1").text(payment);

                    $("#logisticsName1").text(data.data.logisticsName);

                    var delivery = formatTime(data.data.deliveryTime)
                    $("#deliveryTime1").text(delivery);

                    $("#chet1").empty();
                    $("#chet1").append(
                        '<button type = "button" class= "layui-btn layui-btn-normal layui-btn-radius" onclick = "toGoods(' + data.data.orderID + ')" >紧急发货</button >'
                    );
                }
                if (data.data.logisticsType == 3) {
                    $("#Sendout1").css("color", "blue");
                    $("#Sendout2").css("color", "blue");
                    $("#Sendout3").css("border-color", "blue");
                    $("#middling1").css("color", "blue");
                    $("#middling2").css("color", "blue");
                    $("#middling3").css("border-color", "blue");
                    $("#Goods1").css("color", "blue");
                    $("#Goods2").css("color", "blue");
                    $("#orderNumber1").text(data.data.orderNumber);
                    var etal = formatdate(data.data.eTA)
                    $("#eTA1").text(etal);
                    var creation= formatTime(data.data.creationTime)
                    $("#creationTime1").text(creation);
                    var order = formatTime(data.data.orderTime)
                    $("#orderTime1").text(order);
                    var payment = formatTime(data.data.paymentTime)
                    $("#paymentTime1").text(payment);
                    $("#logisticsName1").text(data.data.logisticsName);
                    var delivery = formatTime(data.data.deliveryTime)
                    $("#deliveryTime1").text(delivery);

                    $("#chet1").empty();
                    $("#chet1").append(
                        '<button type="button" class="layui-btn layui-btn-normal layui-btn-radius" onclick="topstorage()">添加入库信息</button>'
                    );
                   
                }
            }
        });
    })
}
//发货
function toSendout(ID) {
    $.ajax({
        type: "Post",
        url: "/Supplier/LedgerInfoLogisticsInTransit",  // 替换为实际的后台删除数据接口地址
        data: { data: JSON.stringify({ orderID: ID }), token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            if (res.code >= 1) {
                layer.alert("发货成功");
                LogisticsFan();  // 重新加载数据，刷新表格显示
            } else {
                layer.alert("发货失败");
            }
        }
    });
}
//送货
function toGoods(ID) {
    $.ajax({
        type: "Post",
        url: "/Supplier/LedgerInfoLogisticsSignFor",  // 替换为实际的后台删除数据接口地址
        data: { data: JSON.stringify({ orderID: ID }), token: localStorage.getItem("token") },
        success: function (res) {
            var res = JSON.parse(res)
            if (res.code >= 1) {
                layer.alert("送货成功");
                LogisticsFan();  // 重新加载数据，刷新表格显示
            } else {
                layer.alert("送货失败");
            }
        }
    });
}
//时间格式
function formatTime(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        var hour = ('0' + date.getHours()).slice(-2);
        var minute = ('0' + date.getMinutes()).slice(-2);
        var second = ('0' + date.getSeconds()).slice(-2);
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
    }
    return "";
}
//时间格式
function formatdate(timeStamp) {
    if (timeStamp) {
        var date = new Date(timeStamp);
        var year = date.getFullYear();
        var month = ('0' + (date.getMonth() + 1)).slice(-2);
        var day = ('0' + date.getDate()).slice(-2);
        return year + '-' + month + '-' + day;
    }
    return "";
}

//入库信息
function topstorage() {
    // 创建表格实例
    $.ajax({
        url: "/Supplier/LedgerInfoStoragesPurchaseNumberShow",
        type: "get",
        data: {
            ProcuredNumber: purchaseNumbers,
        },
        success: function (data) {
            data = JSON.parse(data);
            console.log(data.data);
            if (data.data.length == 0) {
                // 添加
                location.href = "/ProcurementAndStorage/PASaDD";
            } else {
                //显示
                location.href = "/ProcurementAndStorage/PASshow";
            }
        }
    });
}